#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 500m;

/*================================================
 Program: rainfall.do
 Author:  Avi Ebenstein 
 Created: August 2008
 Purpose: Calculate average monthly precipitation at the
          river basin level and the province level.
=================================================*/
  
forvalues i=3/6{;
insheet using ~/pollution/GIS/rainfall/precipitation_by_basinlevel`i'.csv,clear;
capture rename value level`i';                
collapse (mean) rainfall=mean [w=area], by(level`i');
label var rainfall "Average rainfall in (mm/month)";
rename rainfall rainfall`i';                
save ~/research/pollution/datafiles/rainfall_level`i', replace;
              };

use ~/pollution/datafiles/rainfall_level3,clear;
forvalues j=4/6{;
                append using ~/pollution/datafiles/rainfall_level`j';
              };
save ~/pollution/datafiles/rainfall_levels, replace;

use ~/pollution/GIS/county_points/countycentroids_watersheds.dta, replace;
rename provgb provnum;
sort level6;
merge level6 using ~/pollution/datafiles/rainfall_level6;
keep if _merge==3;
collapse (mean) rainfall, by(provnum);
label var rainfall "Monthly mean precipitation (mm/month)";
save ~/pollution/datafiles/rainfall_province, replace;
